<?php
/**
 * Created by PhpStorm.
 * User: keke
 * Date: 2020/8/17
 * Time: 15:59
 */

namespace App\Model;

use Illuminate\Support\Facades\DB;

class FinanceRoomSettle extends BaseModel
{
    protected $table = 'finance_room_settle';
    protected $fields = ['finance_room_settle.*', 'room.room_uid', 'room.name'];

    public function getRoomSettleList($request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $roomUid = $request->input('room_uid');
        $roomName = trim($request->input('room_name'));
        $year = $request->input('year');
        $month = $request->input('month');
        $type = $request->input('status');
        $uuid = $request->input('uuid');
        $where['finance_room_settle.settle_type'] = 0;
        if ($roomUid) {
            $where['room.room_uid'] = $roomUid;
        }
        if ($roomName) {
            $where[] = ['room.name', 'like', "{$roomName}%"];
        }
        if (!empty($year)) {
            $where[] = ['finance_room_settle.month', 'like', "{$year}%"];
        }
        if (!empty($month)) {
            $where[] = ['finance_room_settle.month', 'like', "%{$month}"];
        }

        if (!empty($uuid)) {
            $uinfo = (new Users())->getUserInfoByUuids([$uuid]);
            if(!empty($uinfo[0]['user_id'])){
                $where['finance_room_settle.to_user_id'] = $uinfo[0]['user_id'];
            }

        }

        if (!empty($year) && !empty($month)) {
            $where['finance_room_settle.month'] = intval($year . $month);
        }
        $tab = DB::connection($this->connection)->table($this->table)
            ->leftJoin('room', 'finance_room_settle.room_id', '=', 'room.room_id')
            ->leftJoin('finance_room_support_setting', 'finance_room_settle.room_id', '=', 'finance_room_support_setting.room_id')
            ->where($where);
        if($type){
            $tab = $tab->where('finance_room_settle.status',$type);
        }else{
            $tab = $tab->whereIn('finance_room_settle.status',[0,3]);
        }
        $res = $tab->orderBy('finance_room_settle.id', 'desc')
            ->paginate($limit, ['finance_room_settle.*', 'room.room_uid', 'room.name', 'finance_room_support_setting.support_id'], 'page', $page);
        $res = $this->objToArray($res);
        foreach ($res['data'] as $k => $v) {
            if($v['support_id'] > 0) {
                $supportInfo = DB::connection($this->connection)->table('finance_room_support')
                    ->select('support_name')
                    ->where('finance_room_support.id', '=', $v['support_id'])
                    ->first();
                $res['data'][$k]['support_name'] = $supportInfo->support_name;
            }else {
                $res['data'][$k]['support_name'] = '';
            }
        }
        return $res;
    }


    public function roomSettleApproved($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/confirm', $data, 'urlencoded', 'GET');
    }


    public function roomSettleApprovedAll($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/confirm', $data, 'urlencoded', 'GET');
    }


    public function roomSettleReject($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/reject', $data, 'urlencoded', 'GET');
    }

    public function roomSettleFreeze($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/freeze', $data, 'urlencoded', 'GET');
    }

    public function roomSettleunfreeze($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/unfreeze', $data, 'urlencoded', 'GET');
    }

    public function setFinalDiamond($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/modifyDiamond', $data, 'urlencoded', 'GET');
    }


    public function setRejectDesc($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/modifyDesc', $data, 'urlencoded', 'GET');
    }


    public function getRoomSettlementList($request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $year = $request->input('year');
        $month = $request->input('month');
        $where['finance_room_settle.settle_type'] = 1;
        if (!empty($year)) {
            $where[] = ['finance_room_settle.month', 'like', "{$year}%"];
        }
        if (!empty($month)) {
            $where[] = ['finance_room_settle.month', 'like', "%{$month}"];
        }
        if (!empty($year) && !empty($month)) {
            $where['finance_room_settle.month'] = intval($year . $month);
        }

        return DB::connection($this->connection)->table($this->table)
            ->select('month', DB::raw('sum(diamond) as total_diamond'), DB::raw('sum(final_diamond) as after_diamond'), DB::raw('sum(status) as new_status'))
            ->where($where)
            ->groupBy('month')
            ->orderBy('month', 'desc')
            ->paginate($limit, '', 'page', $page);
    }


    public function createNewsList()
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/makeSettle', '', 'urlencoded', 'GET');
    }


    public function getRoomSettleDetail($request, $month)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $roomUid = $request->input('room_uid');
        $roomName = trim($request->input('room_name'));
        $status = $request->input('status');
        $where['finance_room_settle.settle_type'] = 1;
        $where['finance_room_settle.month'] = $month;
        if ($roomUid) {
            $where['room.room_uid'] = $roomUid;
        }
        if ($roomName) {
            $where[] = ['room.name', 'like', "{$roomName}%"];
        }
        if (intval($status) >= 0) {
            $where['finance_room_settle.status'] = intval($status);
        }

        return DB::connection($this->connection)->table($this->table)
            ->leftJoin('room', 'finance_room_settle.room_id', '=', 'room.room_id')
            ->where($where)
            ->orderBy('finance_room_settle.id', 'desc')
            ->paginate($limit, $this->fields, 'page', $page);
    }


    public function settleThisMonth()
    {
        return curl_request(getKkaudioApiUrl() . '/inner/finance/settle/confirmSettle', '', 'urlencoded', 'GET');
    }
}
